<template>
  <div>
    <el-form :model="orderDetail" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="收货人：" prop="name">
        <el-input size="small" v-model="orderDetail.receiverName"></el-input>
      </el-form-item>
      <el-form-item label="手机：" prop="name">
        <el-input size="small" v-model="orderDetail.receiverMobile"></el-input>
      </el-form-item>
      <el-form-item label="修改地址：" prop="resource">
        <el-radio-group v-model="flag">
          <el-radio label="否"></el-radio>
          <el-radio label="是"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="flag === '是'" label="地址：" prop="name">
        <el-button type="text" disabled>{{orderDetail.receiverProvince}}{{orderDetail.receiverCity}}{{orderDetail.receiverRegion}} {{orderDetail.receiverAddress}}</el-button>
      </el-form-item>
      <el-form-item v-if="flag === '是'" label="新地址：" prop="name">
        <el-select @change="provinceChange" size="small" v-model="orderDetail.receiverProvince" placeholder="省">
          <el-option
            v-for="item in province"
            :key="item.name"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
        <el-select @change="cityChange" size="small" v-model="orderDetail.receiverCity" placeholder="市">
          <el-option
            v-for="item in city"
            :key="item.name"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
        <el-select size="small" v-model="orderDetail.receiverRegion" placeholder="区">
          <el-option
            v-for="item in region"
            :key="item.name"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
        <el-input size="small" v-model="orderDetail.receiverAddress"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" type="primary" @click="submit">保 存</el-button>
      <el-button size="small" @click="close">取 消</el-button>

    </div>
  </div>
</template>

<script>
  import api from "@/api/order.service.js";

  export default {
    name: 'receiverInfo',
    data() {
      return {
        flag: '否',
        rules: {},
        province: [], // 省份数据
        city: [], // 城市数据
        region: [], // 区县数据
      }
    },
    props: {
      orderDetail: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
    created() {
      this.getAddress()
    },
    methods: {
      getAddress() {
        api.queryAreasByParentId({
          parentId: 1
        }).then(res => {
          this.province = res.values
        })
      },
      close(flag) {
        this.$emit('close', flag)
      },
      provinceChange(val) { // 省份变化
        this.orderDetail.receiverCity = null
        this.orderDetail.receiverRegion = null
        this.orderDetail.receiverAddress = null
        let parentId
        this.province.map(res => {
          if (res.name === val) {
            parentId = res.id
          }
        })
        api.queryAreasByParentId({
          parentId
        }).then(res => {
          this.city = res.values
        })
      },
      cityChange(val) {
        this.orderDetail.receiverRegion = null
        this.orderDetail.receiverAddress = null
        let parentId
        this.city.map(res => {
          if (res.name === val) {
            parentId = res.id
          }
        })
        api.queryAreasByParentId({
          parentId
        }).then(res => {
          this.region = res.values
        })
      },
      submit() {
        api.updateOrderAddress(this.orderDetail).then(res => {
          if (res.code == 200) {
            this.$message({
              message: this.$t('common.operateSucccess'),
              type: 'success'
            })
            this.close(true)
          }
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select {
    width: 150px;
  }
.dialog-footer {
  text-align: center;
}
</style>
